package com.stylefeng.guns.modular.ipmng.controller;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.ipmng.dao.BdapWhiteipListMapper;
import com.stylefeng.guns.modular.ipmng.model.BdapWhiteipList;
import com.stylefeng.guns.modular.ipmng.warpper.WhiteIpWrapper;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * IP白名单管理控制器
 *
 * @author xiongyao
 * @Date 2017-11-15 10:08:11
 */
@Controller
@RequestMapping("/ipWhiteList")
public class IpWhiteListController extends BaseController {

    private String PREFIX = "/ipmng/ipWhiteList/";

    @Resource
    BdapWhiteipListMapper mapper;
    
    /**
     * 跳转到IP白名单管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "ipWhiteList.html";
    }

    /**
     * 跳转到添加IP白名单管理
     */
    @RequestMapping("/ipWhiteList_add")
    public String ipWhiteListAdd() {
        return PREFIX + "ipWhiteList_add.html";
    }

    /**
     * 跳转到修改IP白名单管理
     */
    @RequestMapping("/ipWhiteList_update/{ipWhiteListId}")
    public String ipWhiteListUpdate(@PathVariable Integer ipWhiteListId, Model model) {
    	BdapWhiteipList ipWhiteList= mapper.selectById(ipWhiteListId);
    	model.addAttribute("id", ipWhiteList.getId());
    	model.addAttribute("ip", ipWhiteList.getIp());
    	model.addAttribute("name", ipWhiteList.getName());
        return PREFIX + "ipWhiteList_edit.html";
    }

    /**
     * 获取IP白名单管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String ip) {
    	EntityWrapper<BdapWhiteipList> wrapper=new EntityWrapper<BdapWhiteipList>();
    	if(ToolUtil.isNotEmpty(ip)){
    		wrapper.like("ip", ip);
    	}
    	List<BdapWhiteipList> bdapWhiteipLists=mapper.selectList(wrapper);
        return new WhiteIpWrapper(bdapWhiteipLists).warp();
    }

    /**
     * 新增IP白名单管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(String name,String ip) {
    	if(ToolUtil.isNotEmpty(name)&&ToolUtil.isNotEmpty(ip)){
    		BdapWhiteipList bdapWhiteipList=new BdapWhiteipList();
    		bdapWhiteipList.setName(name);
    		bdapWhiteipList.setIp(ip);
    		int i= mapper.insert(bdapWhiteipList);
    		if(i>0){
    			return SUCCESS_TIP;
    		}else{
    			return ERROR;
    		}
    	}else{
    		return ERROR;
    	}
    }

    /**
     * 删除IP白名单管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(String ipWhiteListId) {
    	int i =  mapper.deleteById(Double.parseDouble(ipWhiteListId));
    	if(i==1){
    		return SUCCESS_TIP;
    	}else{
    		return ERROR;
    	}
    }


    /**
     * 修改IP白名单管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(String name,String ip,String id) {
    	
    	if(ToolUtil.isNotEmpty(name)&&ToolUtil.isNotEmpty(ip)&&ToolUtil.isNotEmpty(id)){
    		BdapWhiteipList bdapWhiteipList=new BdapWhiteipList();
    		bdapWhiteipList.setName(name);
    		bdapWhiteipList.setIp(ip);
    		bdapWhiteipList.setId(Double.parseDouble(id));
    		int i= mapper.updateById(bdapWhiteipList);
    		if(i==1){
    			return SUCCESS_TIP;
    		}else{
    			return ERROR;
    		}
    	}else{
    		return ERROR;
    	}
    }

    /**
     * IP白名单管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
